{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.18 \u5229\u7528Mixins\u6269\u5c55\u7c7b\u529f\u80fd\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u5f88\u591a\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u60f3\u4f7f\u7528\u5b83\u4eec\u6765\u6269\u5c55\u5176\u4ed6\u7c7b\u7684\u529f\u80fd\u3002\u4f46\u662f\u8fd9\u4e9b\u7c7b\u5e76\u6ca1\u6709\u4efb\u4f55\u7ee7\u627f\u7684\u5173\u7cfb\u3002\n\u56e0\u6b64\u4f60\u4e0d\u80fd\u7b80\u5355\u7684\u5c06\u8fd9\u4e9b\u65b9\u6cd5\u653e\u5165\u4e00\u4e2a\u57fa\u7c7b\uff0c\u7136\u540e\u88ab\u5176\u4ed6\u7c7b\u7ee7\u627f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u5f53\u4f60\u60f3\u81ea\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\u4f1a\u78b0\u4e0a\u8fd9\u4e9b\u95ee\u9898\u3002\u53ef\u80fd\u662f\u67d0\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u7840\u7c7b\uff0c\n\u4f60\u53ef\u4ee5\u5229\u7528\u5b83\u4eec\u6765\u6784\u9020\u4f60\u81ea\u5df1\u7684\u7c7b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u4f60\u60f3\u6269\u5c55\u6620\u5c04\u5bf9\u8c61\uff0c\u7ed9\u5b83\u4eec\u6dfb\u52a0\u65e5\u5fd7\u3001\u552f\u4e00\u6027\u8bbe\u7f6e\u3001\u7c7b\u578b\u68c0\u67e5\u7b49\u7b49\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6df7\u5165\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class LoggedMappingMixin:\n    \"\"\"\n    Add logging to get/set/delete operations for debugging.\n    \"\"\"\n    __slots__ = ()  # \u6df7\u5165\u7c7b\u90fd\u6ca1\u6709\u5b9e\u4f8b\u53d8\u91cf\uff0c\u56e0\u4e3a\u76f4\u63a5\u5b9e\u4f8b\u5316\u6df7\u5165\u7c7b\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\n\n    def __getitem__(self, key):\n        print('Getting ' + str(key))\n        return super().__getitem__(key)\n\n    def __setitem__(self, key, value):\n        print('Setting {} = {!r}'.format(key, value))\n        return super().__setitem__(key, value)\n\n    def __delitem__(self, key):\n        print('Deleting ' + str(key))\n        return super().__delitem__(key)\n\n\nclass SetOnceMappingMixin:\n    '''\n    Only allow a key to be set once.\n    '''\n    __slots__ = ()\n\n    def __setitem__(self, key, value):\n        if key in self:\n            raise KeyError(str(key) + ' already set')\n        return super().__setitem__(key, value)\n\n\nclass StringKeysMappingMixin:\n    '''\n    Restrict keys to strings only\n    '''\n    __slots__ = ()\n\n    def __setitem__(self, key, value):\n        if not isinstance(key, str):\n            raise TypeError('keys must be strings')\n        return super().__setitem__(key, value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b\u7c7b\u5355\u72ec\u4f7f\u7528\u8d77\u6765\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u4e8b\u5b9e\u4e0a\u5982\u679c\u4f60\u53bb\u5b9e\u4f8b\u5316\u4efb\u4f55\u4e00\u4e2a\u7c7b\uff0c\u9664\u4e86\u4ea7\u751f\u5f02\u5e38\u5916\u6ca1\u4efb\u4f55\u4f5c\u7528\u3002\n\u5b83\u4eec\u662f\u7528\u6765\u901a\u8fc7\u591a\u7ee7\u627f\u6765\u548c\u5176\u4ed6\u6620\u5c04\u5bf9\u8c61\u6df7\u5165\u4f7f\u7528\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class LoggedDict(LoggedMappingMixin, dict):\n    pass\n\nd = LoggedDict()\nd['x'] = 23\nprint(d['x'])\ndel d['x']\n\nfrom collections import defaultdict\n\nclass SetOnceDefaultDict(SetOnceMappingMixin, defaultdict):\n    pass\n\n\nd = SetOnceDefaultDict(list)\nd['x'].append(2)\nd['x'].append(3)\n# d['x'] = 23  # KeyError: 'x already set'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u6df7\u5165\u7c7b\u8ddf\u5176\u4ed6\u5df2\u5b58\u5728\u7684\u7c7b(\u6bd4\u5982dict\u3001defaultdict\u548cOrderedDict)\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\uff0c\u4e00\u4e2a\u63a5\u4e00\u4e2a\u3002\n\u7ed3\u5408\u540e\u5c31\u80fd\u53d1\u6325\u6b63\u5e38\u529f\u6548\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6df7\u5165\u7c7b\u5728\u6807\u51c6\u5e93\u4e2d\u5f88\u591a\u5730\u65b9\u90fd\u51fa\u73b0\u8fc7\uff0c\u901a\u5e38\u90fd\u662f\u7528\u6765\u50cf\u4e0a\u9762\u90a3\u6837\u6269\u5c55\u67d0\u4e9b\u7c7b\u7684\u529f\u80fd\u3002\n\u5b83\u4eec\u4e5f\u662f\u591a\u7ee7\u627f\u7684\u4e00\u4e2a\u4e3b\u8981\u7528\u9014\u3002\u6bd4\u5982\uff0c\u5f53\u4f60\u7f16\u5199\u7f51\u7edc\u4ee3\u7801\u65f6\u5019\uff0c\n\u4f60\u4f1a\u7ecf\u5e38\u4f7f\u7528 socketserver \u6a21\u5757\u4e2d\u7684 ThreadingMixIn \u6765\u7ed9\u5176\u4ed6\u7f51\u7edc\u76f8\u5173\u7c7b\u589e\u52a0\u591a\u7ebf\u7a0b\u652f\u6301\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7684XML-RPC\u670d\u52a1\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xmlrpc.server import SimpleXMLRPCServer\nfrom socketserver import ThreadingMixIn\nclass ThreadedXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u65f6\u5728\u4e00\u4e9b\u5927\u578b\u5e93\u548c\u6846\u67b6\u4e2d\u4e5f\u4f1a\u53d1\u73b0\u6df7\u5165\u7c7b\u7684\u4f7f\u7528\uff0c\u7528\u9014\u540c\u6837\u662f\u589e\u5f3a\u5df2\u5b58\u5728\u7684\u7c7b\u7684\u529f\u80fd\u548c\u4e00\u4e9b\u53ef\u9009\u7279\u5f81\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6df7\u5165\u7c7b\uff0c\u6709\u51e0\u70b9\u9700\u8981\u8bb0\u4f4f\u3002\u9996\u5148\u662f\uff0c\u6df7\u5165\u7c7b\u4e0d\u80fd\u76f4\u63a5\u88ab\u5b9e\u4f8b\u5316\u4f7f\u7528\u3002\n\u5176\u6b21\uff0c\u6df7\u5165\u7c7b\u6ca1\u6709\u81ea\u5df1\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4eec\u5e76\u6ca1\u6709\u5b9a\u4e49 __init__() \u65b9\u6cd5\uff0c\u5e76\u4e14\u6ca1\u6709\u5b9e\u4f8b\u5c5e\u6027\u3002\n\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u5728\u4e0a\u9762\u660e\u786e\u5b9a\u4e49\u4e86 __slots__ = () \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u79cd\u5b9e\u73b0\u6df7\u5165\u7c7b\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528\u7c7b\u88c5\u9970\u5668\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def LoggedMapping(cls):\n    \"\"\"\u7b2c\u4e8c\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u7c7b\u88c5\u9970\u5668\"\"\"\n    cls_getitem = cls.__getitem__\n    cls_setitem = cls.__setitem__\n    cls_delitem = cls.__delitem__\n\n    def __getitem__(self, key):\n        print('Getting ' + str(key))\n        return cls_getitem(self, key)\n\n    def __setitem__(self, key, value):\n        print('Setting {} = {!r}'.format(key, value))\n        return cls_setitem(self, key, value)\n\n    def __delitem__(self, key):\n        print('Deleting ' + str(key))\n        return cls_delitem(self, key)\n\n    cls.__getitem__ = __getitem__\n    cls.__setitem__ = __setitem__\n    cls.__delitem__ = __delitem__\n    return cls\n\n\n@LoggedMapping\nclass LoggedDict(dict):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u6548\u679c\u8ddf\u4e4b\u524d\u7684\u662f\u4e00\u6837\u7684\uff0c\u800c\u4e14\u4e0d\u518d\u9700\u8981\u4f7f\u7528\u591a\u7ee7\u627f\u4e86\u3002\u53c2\u80039.12\u5c0f\u8282\u83b7\u53d6\u66f4\u591a\u7c7b\u88c5\u9970\u5668\u7684\u4fe1\u606f\uff0c\n\u53c2\u80038.13\u5c0f\u8282\u67e5\u770b\u66f4\u591a\u6df7\u5165\u7c7b\u548c\u7c7b\u88c5\u9970\u5668\u7684\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}